package com.bestcem.xm.user.service.impl;

import cn.hutool.core.lang.ObjectId;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.enums.ReqErrorCodeEnum;
import com.bestcem.xm.common.core.enums.ResourceErrorCodeEnum;
import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.common.core.uitls.SignUtil;
import com.bestcem.xm.user.config.UserSignConfig;
import com.bestcem.xm.user.controller.param.UpdateUnsubscribeSettingParam;
import com.bestcem.xm.user.dao.UnsubscribeSettingDao;
import com.bestcem.xm.user.entity.pojo.UnsubscribeSettingDO;
import com.bestcem.xm.user.enums.UnsubscribeTypeEnum;
import com.bestcem.xm.user.service.UnsubscribeSettingService;
import com.bestcem.xm.user.service.dto.setting.UnsubscribeSettingDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 退订设置服务实现类
 *
 * @author ympan <yuming.pan@idiaoyan.com>
 * @version v1.0
 * @date 2022/7/12 14:45
 */
@Slf4j
@Service
public class UnsubscribeSettingServiceImpl implements UnsubscribeSettingService {
    @Resource
    private UnsubscribeSettingDao unsubscribeSettingDao;

    @Resource
    private UserSignConfig userSignConfig;

    @Override
    public ServiceResult<String> updateUnsubscribeSetting(UpdateUnsubscribeSettingParam param) {
        // 根据orgId获取退订设置
        String orgId = param.getOrgId();
        UnsubscribeSettingDO unsubscribeSettingDO = unsubscribeSettingDao.getByOrgId(orgId);
        if (Objects.isNull(unsubscribeSettingDO)) {
            return ServiceResult.fail(ResourceErrorCodeEnum.NOT_FOUND);
        }
        // 设置更新参数
        unsubscribeSettingDO.setType(param.getType());
        if (UnsubscribeTypeEnum.PERMANENT.getType().equals(param.getType())) {
            unsubscribeSettingDO.setDuration(null);
        } else {
            unsubscribeSettingDO.setDuration(param.getDuration());
        }
        unsubscribeSettingDO.setOperatorId(param.getUserId());
        unsubscribeSettingDO.setUpdateTime(DateUtil.getCommonDate());
        // 更新退订设置
        unsubscribeSettingDao.updateById(unsubscribeSettingDO);
        return ServiceResult.success(unsubscribeSettingDO.getId());
    }

    @Override
    public ServiceResult<String> insert(String orgId) {
        if (!ObjectId.isValid(orgId)) {
            log.error("【用户】插入退订设置失败 orgId格式错误，orgId:{}", orgId);
            ServiceResult.fail(ReqErrorCodeEnum.PARAM_FORMAT);
        }
        UnsubscribeSettingDO unsubscribeSettingDO = new UnsubscribeSettingDO();
        String id = ObjectId.next();
        unsubscribeSettingDO.setId(id);
        unsubscribeSettingDO.setOrgId(orgId);
        unsubscribeSettingDO.setType(UnsubscribeTypeEnum.PERMANENT.getType());
        unsubscribeSettingDO.setDuration(null);
        unsubscribeSettingDO.setCreatorId(null);
        unsubscribeSettingDO.setOperatorId(null);
        unsubscribeSettingDO.setCreateTime(DateUtil.getCommonDate());
        unsubscribeSettingDO.setUpdateTime(DateUtil.getCommonDate());
        unsubscribeSettingDao.insert(unsubscribeSettingDO);
        return ServiceResult.success(id);
    }

    @Override
    public ServiceResult<UnsubscribeSettingDTO> getByOrgId(String orgId) {
        UnsubscribeSettingDO unsubscribeSettingDO = unsubscribeSettingDao.getByOrgId(orgId);
        return ServiceResult.success(UnsubscribeSettingDTO.do2Dto(unsubscribeSettingDO));
    }

    @Override
    public ServiceResult<String> genSig(String orgId) {
        String signKey = userSignConfig.getSettingKey();
        Map<String, Object> map = new HashMap<>();
        map.put("orgId", orgId);
        return ServiceResult.success(SignUtil.genSign(map, signKey));
    }
}
    